import axios from 'axios';
import axiosRetry from 'axios-retry';

// 超时处理，重新发送请求
axiosRetry(axios, {
	retries: 10,
	retryDelay: () => 1000,
	shouldResetTimeout: true,
	retryCondition: (error) => {
		throw error;
		if (error.message.includes("timeout") || error.message.includes('status code')) {
			return true;
		} else {
			return false;
		};
	}
});

// 请求拦截
axios.interceptors.request.use((config) => {
	return config;
}, (error) => {
	throw error;
	return Promise.reject(error);
})

// 响应拦截
axios.interceptors.response.use((response) => {
	return response;
}, (err) => {
	if(err){
		switch(err.response.status){
			case 400:
				break;
		}
	}else{
		console.log('可能断网了')
	}
	throw err;
	return Promise.resolve(err.response);
})

// axios发送get请求，url地址，params请求头参数，method请求方式
export const request = async (url, params, method) => {
	try{
		let res = await axios[method](url, params);
		return res&&res.data.data;
	}catch(e){
		throw e;
		console.log(e);
	}
}